<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猫咖管理系统 - 留言板</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* 全局样式 */
        :root {
            --primary-color: #D85D5D;
            --secondary-color: #9B7E46;
            --accent-color: #FFA384;
            --light-color: #F8F3EA;
            --dark-color: #333333;
            --success-color: #4CAF50;
            --error-color: #FF5252;
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f9f9f9;
            color: var(--dark-color);
            line-height: 1.6;
        }

        .container {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
        }

        /* 顶部导航栏样式 */
        header {
            background-color: white;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .header-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 0;
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .logo-icon {
            width: 40px;
            height: 40px;
            background-color: var(--primary-color);
            border-radius: 50%;
            position: relative;
        }

        .logo-icon::after {
            content: '';
            position: absolute;
            top: -4px;
            left: -4px;
            width: 48px;
            height: 48px;
            border-radius: 50%;
            border: 1px solid rgba(216, 93, 93, 0.2);
        }

        .logo h1 {
            font-size: 24px;
            color: var(--primary-color);
        }

        nav ul {
            display: flex;
            list-style: none;
            gap: 25px;
        }

        nav a {
            text-decoration: none;
            color: var(--dark-color);
            font-weight: 500;
            padding: 8px 12px;
            border-radius: 4px;
            transition: var(--transition);
            position: relative;
        }

        nav a:hover,
        nav a.active {
            color: var(--primary-color);
        }

        nav a::after {
            content: '';
            position: absolute;
            bottom: -5px;
            left: 0;
            width: 0;
            height: 2px;
            background-color: var(--primary-color);
            transition: var(--transition);
        }

        nav a:hover::after,
        nav a.active::after {
            width: 100%;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-info span {
            font-weight: 500;
        }

        .btn {
            padding: 8px 16px;
            border-radius: 4px;
            border: none;
            cursor: pointer;
            font-weight: 500;
            transition: var(--transition);
        }

        .btn-primary {
            background-color: var(--primary-color);
            color: white;
        }

        .btn-primary:hover {
            background-color: #c74d4d;
        }

        /* 留言板内容区域 */
        .message-board {
            padding: 60px 0;
            background-color: #fff;
            min-height: calc(100vh - 200px);
        }

        .section-title {
            text-align: center;
            margin-bottom: 40px;
            position: relative;
        }

        .section-title h2 {
            font-size: 32px;
            color: var(--primary-color);
            display: inline-block;
            margin-bottom: 15px;
        }

        .section-title::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 80px;
            height: 3px;
            background-color: var(--secondary-color);
        }

        /* 留言表单样式 */
        .message-form {
            background-color: var(--light-color);
            border-radius: 12px;
            padding: 30px;
            margin-bottom: 40px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: var(--dark-color);
        }

        .form-group input,
        .form-group textarea {
            width: 100%;
            padding: 14px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 1rem;
            transition: var(--transition);
            background-color: white;
        }

        .form-group input:focus,
        .form-group textarea:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(216, 93, 93, 0.2);
        }

        .form-group textarea {
            min-height: 120px;
            resize: vertical;
        }

        .submit-btn {
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 14px 25px;
            font-size: 1.1rem;
            font-weight: 600;
            border-radius: 8px;
            cursor: pointer;
            transition: var(--transition);
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            width: 100%;
        }

        .submit-btn:hover {
            background-color: #c74d4d;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(216, 93, 93, 0.3);
        }

        /* 留言列表样式 */
        .messages-container {
            display: flex;
            flex-direction: column;
            gap: 25px;
        }

        .message-card {
            background-color: white;
            border-radius: 12px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            border-left: 4px solid var(--accent-color);
            transition: var(--transition);
        }

        .message-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
        }

        .message-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .avatar {
            width: 50px;
            height: 50px;
            background: linear-gradient(45deg, var(--primary-color), var(--accent-color));
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            font-size: 22px;
        }

        .user-details {
            display: flex;
            flex-direction: column;
        }

        .username {
            font-weight: 600;
            color: var(--dark-color);
            font-size: 18px;
        }

        .nickname {
            color: var(--secondary-color);
            font-size: 14px;
        }

        .message-time {
            color: #95a5a6;
            font-size: 14px;
            font-style: italic;
        }

        .message-content {
            color: #555;
            line-height: 1.7;
            font-size: 16px;
        }

        .empty-state {
            text-align: center;
            padding: 50px 20px;
            background-color: white;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
        }

        .empty-state i {
            font-size: 48px;
            color: var(--accent-color);
            margin-bottom: 20px;
        }

        .empty-state h3 {
            font-size: 24px;
            color: var(--primary-color);
            margin-bottom: 15px;
        }

        .empty-state p {
            color: #666;
            max-width: 500px;
            margin: 0 auto;
        }

        /* 页脚样式 */
        footer {
            background-color: var(--dark-color);
            color: white;
            padding: 40px 0 20px;
        }

        .footer-content {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 30px;
            margin-bottom: 30px;
        }

        .footer-column h3 {
            font-size: 20px;
            margin-bottom: 20px;
            position: relative;
            padding-bottom: 10px;
        }

        .footer-column h3::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 50px;
            height: 2px;
            background-color: var(--primary-color);
        }

        .footer-links {
            list-style: none;
        }

        .footer-links li {
            margin-bottom: 10px;
        }

        .footer-links a {
            color: #bbb;
            text-decoration: none;
            transition: var(--transition);
        }

        .footer-links a:hover {
            color: white;
            padding-left: 5px;
        }

        .contact-info {
            color: #bbb;
        }

        .contact-info p {
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .copyright {
            text-align: center;
            padding-top: 20px;
            border-top: 1px solid #444;
            color: #bbb;
            font-size: 14px;
        }
        .messages-scroll-container {
            max-height: 60vh;
            overflow-y: auto;
            padding-right: 10px;
        }

        .messages-scroll-container::-webkit-scrollbar {
            width: 8px;
        }
        .messages-scroll-container::-webkit-scrollbar-thumb {
            background-color: rgba(0, 0, 0, 0.2);
            border-radius: 4px;
        }
        .messages-scroll-container::-webkit-scrollbar-track {
            background-color: transparent;
        }


        /* 响应式设计 */
        @media (max-width: 768px) {
            .header-container {
                flex-direction: column;
                gap: 15px;
            }

            nav ul {
                flex-wrap: wrap;
                justify-content: center;
                gap: 10px;
            }

            .message-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;
            }

            .message-time {
                align-self: flex-end;
            }
        }
    </style>
</head>
<body>
<!-- 顶部导航栏 -->
<header>
    <div class="container header-container">
        <div class="logo">
            <div class="logo-icon"></div>
            <h1>猫咖管理系统</h1>
        </div>

        <nav>
            <ul>
                <li><a href="index.html">首页</a></li>
                <li><a href="cat.html">猫咪预约</a></li>
                <li><a href="cafe.html">咖啡购买</a></li>
                <li><a href="center.html">个人中心</a></li>
                <li><a href="message.html" class="active">留言板</a></li>
<!--                <li><a href="shoppingcart.html">购物车</a></li>-->
                <li><a href="orders.html">订单信息</a></li>
                <li><a href="booking.html">预约信息</a></li>
            </ul>
        </nav>

        <!-- 顶部导航栏的用户信息部分 -->
        <div class="user-info">
            <span id="user-display">未登录</span>
            <button class="btn btn-primary" id="logout-btn">退出</button>
        </div>
    </div>
</header>

<!-- 留言板内容区域 -->
<section class="message-board">
    <div class="container">
        <div class="section-title">
            <h2>猫咖留言板</h2>
            <p>分享您与猫咪的美好时光，留下您的宝贵建议</p>
        </div>

        <!-- 留言表单 -->
        <div class="message-form">
            <form id="messageForm">
                <div class="form-group" style="display: none">
                    <label for="username">用户名 *</label>
                    <input type="text" id="username" value="小美" readonly>
                </div>

<!--                <div class="form-group">-->
<!--                    <label for="nickname">昵称</label>-->
<!--                    <input type="text" id="nickname" placeholder="请输入您的昵称（可选）">-->
<!--                </div>-->

                <div class="form-group">
                    <label for="content">留言内容 *</label>
                    <textarea id="content" placeholder="请输入您的留言内容..." required></textarea>
                </div>

                <button type="submit" class="submit-btn">
                    <i class="fas fa-paper-plane"></i> 发表留言
                </button>
            </form>
        </div>

        <!-- 留言列表 -->
<!--        <div class="messages-container" id="messagesContainer">-->
<!--            &lt;!&ndash; 留言卡片会动态加载到这里 &ndash;&gt;-->
<!--            <div class="message-card">-->
<!--                <div class="message-header">-->
<!--                    <div class="user-info">-->
<!--                        <div class="avatar">猫</div>-->
<!--                        <div class="user-details">-->
<!--                            <div class="username">猫咪爱好者</div>-->
<!--                            <div class="nickname">喵星人</div>-->
<!--                        </div>-->
<!--                    </div>-->
<!--                    <div class="message-time">2025-06-15 14:30</div>-->
<!--                </div>-->
<!--                <div class="message-content">-->
<!--                    店里的布偶猫真是太可爱了！性格温顺又亲人，咖啡也很香醇。已经推荐给朋友了，下次还会再来！-->
<!--                </div>-->
<!--            </div>-->

<!--            <div class="message-card">-->
<!--                <div class="message-header">-->
<!--                    <div class="user-info">-->
<!--                        <div class="avatar">咖</div>-->
<!--                        <div class="user-details">-->
<!--                            <div class="username">咖啡达人</div>-->
<!--                            <div class="nickname">拿铁控</div>-->
<!--                        </div>-->
<!--                    </div>-->
<!--                    <div class="message-time">2025-06-12 10:15</div>-->
<!--                </div>-->
<!--                <div class="message-content">-->
<!--                    这里的咖啡品质真的超出预期！猫咪主题拉花很有创意，环境也很舒适。特别推荐他们的招牌猫爪拿铁，味道很特别。-->
<!--                </div>-->
<!--            </div>-->
<!--        </div>-->
        <!-- 留言列表 -->
        <div class="messages-scroll-container">
            <div class="messages-container" id="messagesContainer">
                <!-- 留言卡片会动态加载到这里 -->
            </div>
        </div>

    </div>
</section>

<!-- 页脚 -->
<footer>
    <div class="container">
        <div class="footer-content">
            <div class="footer-column">
                <h3>关于我们</h3>
                <p>猫咖管理系统致力于为猫咪咖啡馆提供专业的管理解决方案，帮助店主高效管理猫咪、咖啡和顾客信息。</p>
            </div>

            <div class="footer-column">
                <h3>快速链接</h3>
                <ul class="footer-links">
                    <li><a href="index.html">首页</a></li>
                    <li><a href="cat.html">猫咪信息</a></li>
                    <li><a href="cafe.html">咖啡菜单</a></li>
                    <li><a href="booking.html">在线预订</a></li>
                    <li><a href="center.html">个人中心</a></li>
                </ul>
            </div>

            <div class="footer-column">
                <h3>联系方式</h3>
                <div class="contact-info">
                    <p>地址：广东科技学院</p>
                    <p>电话：666666</p>
                    <p>邮箱：666666.com</p>
                    <p>营业时间：10:00-18:00</p>
                </div>
            </div>
        </div>

        <div class="copyright">
            <p>© 2025 猫咖管理系统 版权所有 | 设计开发：会非的杨</p>
        </div>
    </div>
</footer>

<script>
    const API_BASE_URL = 'http://localhost:8080/catcafe';

    document.addEventListener('DOMContentLoaded', function () {
        const usernameInput = document.getElementById('username');
        //const nicknameInput = document.getElementById('nickname');
        const contentInput = document.getElementById('content');
        const messageForm = document.getElementById('messageForm');
        const messagesContainer = document.getElementById('messagesContainer');

        // // 用户信息初始化
        // const storedUsername = localStorage.getItem('username') || '小美';
        // const storedRole = localStorage.getItem('role') || 'admin';
        // usernameInput.value = storedUsername;
        // document.getElementById('user-display').textContent = `${storedRole === 'admin' ? '管理员' : '用户'}：${storedUsername}`;
        //
        // // 退出功能
        // document.getElementById('logout-btn').addEventListener('click', () => {
        //     localStorage.removeItem('username');
        //     localStorage.removeItem('role');
        //     alert('您已退出');
        //     window.location.href = 'login.html';
        // });

        const username = localStorage.getItem('username');
        const role = localStorage.getItem('role');

        // 获取显示用户信息的元素
        const userDisplay = document.getElementById('user-display');

        // 如果有用户信息，显示在导航栏
        if (username) {
            // 根据角色显示不同的文本
            const roleText = role === 'admin' ? '管理员' : '普通用户';
            userDisplay.textContent = `${roleText}：${username}`;

            // 添加角色图标
            userDisplay.innerHTML = `            <i class="fas fa-user-circle"></i>
            <span class="username-text">${username}</span>
            <span class="role-badge">${roleText}</span>
        `;
        } else {
            userDisplay.textContent = "未登录";
        }

        // 退出按钮功能
        const logoutBtn = document.getElementById('logout-btn');
        logoutBtn.addEventListener('click', function () {
            // 清除存储的用户信息
            localStorage.removeItem('username');
            localStorage.removeItem('role');

            // 提示用户并跳转到登录页
            alert('您已成功退出');
            window.location.href = 'login.html'; // 替换为您的登录页面
        });

        // 表单提交处理
        messageForm.addEventListener('submit', function (e) {
            e.preventDefault();

            const username = usernameInput.value.trim();
            //const nickname = nicknameInput.value.trim() || username;
            const content = contentInput.value.trim();
            const id=localStorage.getItem("userId");

            if (!content) {
                alert('请输入留言内容');
                return;
            }

            const newMessage = {
                username,
                //nickname,
                content,
                id,
                time: new Date().toISOString().slice(0, 19).replace('T', ' ')
            };

            sendMessage(newMessage).then(() => {
                loadMessages(); // 发送成功后刷新留言
            });
        });

        // 发送留言到后端
        async function sendMessage(message) {
            try {
                const response = await fetch(`${API_BASE_URL}/messages`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(message)
                });

                if (!response.ok) {
                    throw new Error('留言失败');
                }

                // 清空表单
                contentInput.value = '';
                //nicknameInput.value = '';
                //alert('留言发表成功！');
            } catch (error) {
                console.error('提交留言出错:', error);
                alert('留言提交失败，请稍后再试。');
            }
        }

        // 从后端加载留言
        async function loadMessages() {
            messagesContainer.innerHTML = `
                <div class="empty-state">
                    <i class="fas fa-spinner fa-spin"></i>
                    <h3>正在加载留言...</h3>
                </div>
            `;

            try {
                const response = await fetch(`${API_BASE_URL}/messages`);
                if (!response.ok) throw new Error('加载留言失败');

                const result = await response.json();
                const messages = result.data; // 根据你的Result类结构调整

                if (!messages || messages.length === 0) {
                    showEmptyState();
                    return;
                }

                renderMessages(messages);
            } catch (error) {
                console.error('获取留言失败:', error);
                messagesContainer.innerHTML = `
                    <div class="empty-state">
                        <i class="fas fa-exclamation-triangle"></i>
                        <h3>加载留言失败</h3>
                        <p>请检查网络连接或稍后重试</p>
                    </div>
                `;
            }
        }

        // 渲染留言
        function renderMessages(messages) {
            messagesContainer.innerHTML = '';

            messages.forEach(message => {
                const card = document.createElement('div');
                card.className = 'message-card';
                card.innerHTML = `
                    <div class="message-header">
                        <div class="user-info">
                            <div class="avatar">${message.username.charAt(0)}</div>
                            <div class="user-details">
                                <div class="username">${message.username}</div>
                                ${message.nickname ? `<div class="nickname">${message.nickname}</div>` : ''}
                            </div>
                        </div>
                        <div class="message-time">${formatTime(message.time)}</div>
                    </div>
                    <div class="message-content">
                        ${message.content}
                    </div>
                `;
                // messagesContainer.appendChild(card);
                messagesContainer.insertBefore(card, messagesContainer.firstChild);
            });
        }

        // 空状态展示
        function showEmptyState() {
            messagesContainer.innerHTML = `
                <div class="empty-state">
                    <i class="far fa-comment-alt"></i>
                    <h3>暂无留言</h3>
                    <p>成为第一个留言的人吧！</p>
                </div>
            `;
        }

        // 时间格式化函数
        function formatTime(isoString) {
            const date = new Date(isoString);
            return date.toLocaleString('zh-CN', {
                year: 'numeric',
                month: '2-digit',
                day: '2-digit',
                hour: '2-digit',
                minute: '2-digit'
            });
        }

        // 初始化加载留言
        loadMessages();
    });
</script>

</body>
</html>